package net.lanelife.casino.cms.web.controller;

import javax.servlet.ServletRequest;

import net.lanelife.casino.caipiao.dto.admin.ActivityCreateDTO;
import net.lanelife.casino.caipiao.dto.admin.ActivityDTO;
import net.lanelife.casino.caipiao.dto.admin.ActivityTable;
import net.lanelife.casino.caipiao.dto.admin.ActivityUpdateDTO;
import net.lanelife.casino.caipiao.entity.Activity;
import net.lanelife.casino.caipiao.service.api.IActivityPrizeTypeService;
import net.lanelife.casino.caipiao.service.api.IActivityService;
import net.lanelife.casino.cms.web.controller.support.CRUDController;
import net.lanelife.casino.common.service.support.IGenericService;
import net.lanelife.casino.common.web.resolver.ViewResolver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = ActivityController.PATH)
public class ActivityController extends CRUDController<Activity, Integer, ActivityDTO, ActivityCreateDTO, ActivityUpdateDTO, ActivityTable<ActivityDTO>> {
	
	public static final String PATH = "/activity";
	
	public ActivityController() {
		super(PATH);
	}

	@Autowired
	private IActivityService activityService;
	
	@Autowired
	private IActivityPrizeTypeService activityPrizeTypeService;
	
	@Override
	protected IGenericService<Activity, Integer> getService() {
		return activityService;
	}
	
	@Override
	protected void preList(Model model, ServletRequest request) throws Exception {
		model.addAttribute("prizeTypeList", activityPrizeTypeService.findAll());
	}
	
	@Override
	protected void preCreate(Model model, ActivityCreateDTO createDTO) throws Exception {
		model.addAttribute("prizeTypeList", activityPrizeTypeService.findAll());
	}

	@Override
	protected void preUpdate(Model model, ActivityUpdateDTO updateDTO) throws Exception {
		model.addAttribute("prizeTypeList", activityPrizeTypeService.findAll());
	}

	@RequestMapping(value = { "/available:{available}/{id}" }, method = RequestMethod.GET)
	public String available(@PathVariable Integer id, @PathVariable Boolean available) throws Exception {
		activityService.update(id, available);
		return ViewResolver.getView(PATH + "/list");
	}

}
